package cn.itsource.basic.base.service.impl;

import cn.itsource.basic.base.mapper.BaseMapper;
import cn.itsource.basic.base.query.BaseQuery;
import cn.itsource.basic.base.service.IBaseService;
import cn.itsource.basic.utli.PageList;
import cn.itsource.org.domain.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<T> querytAll() {
        return baseMapper.selectAll();
    }

    @Override
    public T queryByid(Long id) {
        return baseMapper.selectByid(id);
    }

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delect(Long id) {
        baseMapper.delect(id);
        redisTemplate.opsForValue().set("deptTree",null);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        PageList<T> pageList = new PageList<>();
        //将查询的分页封装到对象中
        pageList.setRows(baseMapper.selectByPage(query));
        //将查询的条数封装到对象中
        pageList.setTotal(baseMapper.count(query));
        return pageList;
    }

    @Override
    @Transactional
    public void batchDeletedelect(List<Long> ids) {
        baseMapper.batchDeletedelect(ids);
        redisTemplate.opsForValue().set("deptTree",null);
    }
}
